<?php

class Admin_PopupController extends Zend_Controller_Action
{
    private $_config = null;
    public function init()
    {
        $this->_config = Utils_Global::getConfig('admin', 'upload');
        $this->view->pageclzz = 'active';
    }
    
    public function editAction() {
        $id = trim(Utils_Global::$params['id']);
        if($id) {//Edit
            $model = Admin_Model_Page::factory();
            $page = $model->getPages(array('id' => $id));
            $page = array_values($page);
            $this->view->page = $page[0];
            $this->view->id = $id;
            $this->view->title = "Chỉnh sửa tin tức";
            $this->view->errMessage = Utils_Global::$params['errMessage'];
            if($this->view->errMessage) {
            	$this->view->page = Utils_Global::$params;
            }
        } else {//Insert
            $this->view->page = Utils_Global::$params;
            $this->view->title = "Tạo tin trang mới";
            $this->view->errMessage = Utils_Global::$params['errMessage'];
        }
    }
    
    public function listAction() {
        $auth = Zend_Auth::getInstance();
        $identity = $auth->getIdentity();
        $userName = $identity->username;
        if($userName == 'admin') {
            $userName = '';
        }
        
        $page = intval(Utils_Global::$params['page']);
        $limit = intval(Utils_Global::$params['limit']);
        $id = Utils_Global::$params['id'];
        $title = Utils_Global::$params['title'];
        $active = trim(Utils_Global::$params['active']);
        $datelineF = strtotime(Utils_Global::$params['datelineF']);
        $datelineT = strtotime(Utils_Global::$params['datelineT']);
        if($datelineT) {
            $datelineT += 3600*24;
        }
        $sortBy = Utils_Global::$params['sort_by'];
        $sortDir = Utils_Global::$params['sort_dir'];
        if($limit <= 0) {
            $limit = 10;
        }
        if($page <= 0) {
            $page = 1;
        }

        $model = Admin_Model_Page::factory();
        $options = array('id' => $id, 'title' => $title, 'active' => $active,
                        'datelineF' => $datelineF, 'datelineT' => $datelineT, 'order' => $sortBy, 'by' => $sortDir?'DESC':'ASC',
                            'offset' => ($page - 1) * $limit, 'limit' => $limit);
        $pages = $model->getPages($options);
        $sortByCol = 'sortBy'.$sortBy;
        $this->view->{$sortByCol} = 1;
        $this->view->sortDir = $sortDir;
        if($sortDir) {
            $this->view->clzzSort = 'icon-chevron-down';
        } else {
            $this->view->clzzSort = 'icon-chevron-up';
        }
        
        $options['sort_by'] = $sortBy;
        $options['sort_dir'] = $sortDir;
        $this->view->params = $options;
        $this->view->pages = $pages;
        $this->view->title = "Quản lý trang";
        $this->view->page = $page;
        $this->view->numRowPerPage = $limit;
        $this->view->totalItem = $model->getPagesCount($options);
        $this->view->currentUrl = $this->view->serverUrl() . $this->view->url(array()) . '?' . http_build_query($options);
        $this->view->errMessage = Utils_Global::$params['errMessage'];
    }
    
    public function saveAction() {
        $id = Utils_Global::$params['id'];
        $enTitle = Utils_Global::$params['en_title'];
        $viTitle = Utils_Global::$params['vi_title'];
        $enContent = Utils_Global::$params['en_content'];
        $viContent = Utils_Global::$params['vi_content'];
        $enTitleSeo = Utils_CommonFunction::getNameSeo($enTitle);
        $viTitleSeo = Utils_CommonFunction::getNameSeo($viTitle);
        $active = trim(Utils_Global::$params['active']);
        
        if($active) {
            $active = 1;
        }
        $auth = Zend_Auth::getInstance();
        $userName = $auth->getIdentity()->username;
        if(!$userName) {
            $this->_redirect('/admin/auth');
        }
        if(!$enTitleSeo) {
        	Utils_Global::$params['errMessage'] = 'Vui lòng nhập tên page hợp lệ!';
        	$this->_forward('edit', 'page', 'admin');
        	return;
        }
        
        if($this->_request->isPost()) {
            //Insert into I18n
            $i18nModel = Admin_Model_I18n::factory();
            $translationModel = Admin_Model_Translation::factory();
            if($id && $enId && $viId) {//Update
            	//Insert into translation
            	$translation = array('seo_title' => $enTitleSeo, 'content' => $enTitle,
            			'last_update' => time());
            	$translationModel->update($enId, $translation);//update en
            	$translation['seo_title'] = $viTitleSeo;
            	$translation['content'] = $viTitle;
            	$translationModel->update($viId, $translation);//update vi
            } else {//Insert
            	 //Insert title
                $i18nId1 = $i18nModel->insert(array());
                Utils_CommonFunction::insertTranslation($enTitle, $enTitleSeo, 'en', $i18nId1);
                Utils_CommonFunction::insertTranslation($viTitle, $viTitleSeo, 'vi', $i18nId1);
                //Insert content
                $i18nId2 = $i18nModel->insert(array());
                Utils_CommonFunction::insertTranslation($enContent, '', 'en', $i18nId2);
                Utils_CommonFunction::insertTranslation($viContent, '', 'vi', $i18nId2);
            }
            
            $data = array();
            $data = array(
            		'i18n_title' => $i18nId1,
            		'i18n_content' => $i18nId2,
            		'last_update' => time(),
                    'active' => $active,
            );
            
            //Save change or insert new
            $model = Admin_Model_Page::factory();
            if($id) {
            	$result = $model->update($id, $data);
            } else {
            	$data['dateline'] = time();
            	$result = $model->insert($data);
            }
            
        }
        if($result > 0) {
        	$params = array('errMessage' => "Thay đổi thành công");
        	$this->_helper->redirector('edit', 'page', 'admin', $params);
        } else {
            Utils_Global::$params['errMessage'] = 'Thao tác không thành công!';
            $this->_forward('edit', 'page', 'admin');
            return;
        }
    }
    
    public function deleteAction() {
        $id = Utils_Global::$params['id'];
        $model = Admin_Model_Page::factory();
        $result = $model->delete($id);
        if($result) {
        	$errMessage = "Xóa thành công";
        } else {
        	$errMessage = "Xóa không thành công, vui lòng thử lại!";
        }
        $this->_redirect('/admin/page/list/?errMessage=' . $errMessage);
    }
    
}

